﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using System.Web;
using YouHP.Model;

namespace YouHP.Pages.Ajax
{
    public class CommentConversationReplyAddAjax : YouHP.Pages.BasePage
    {

        protected CommentInfo CommentData;


        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            CommentData = CommentBLL.Instance.GetCommentInfo(RequestTools.GetQueryInt("commentId", 0));


            CommentInfo info = ValidataForm();
            if (info == null) return;

            //执行添加
            CommentBLL.Instance.AddComment(info);

            //生成XML

            //该留言回复的列表
            List<CommentInfo> CommentReplyList = CommentBLL.Instance.GetAllReplyListByCommentId(CommentData.Id);

            StringBuilder sb = new StringBuilder();

            //主留言
            sb.Append("<li style=\"border-bottom:1px solid #E1E4E6;\"><div style=\"padding-left:20px; text-align:center; float:left;\">");
            string TempUrl = "/Profile/Index.aspx?uid=" + CommentData.PostUserId;
            sb.Append("<a href=\"" + this.GetUrl(TempUrl) + "\"><img class=\"avatar\" width=\"45\" height=\"45\" src=\"" + UserBLL.Instance.GetUserInfo(CommentData.PostUserId).Avatar + "\"/>");
            sb.Append("</a></div>");
            sb.Append("<div style=\"margin:0pt 0px 0pt 82px;\">");
            sb.Append("<div class=\"bar\">");

            sb.Append("<span style=\"float:right;\">" + CommentData.PostTime.ToString("yyyy-MM-dd  HH:mm") + "</span>");
            if (CommentData.PostUserId != UserData.Id)
            {
                sb.Append("<span><a class=\"client_name\" href=\"" + this.GetUrl("/Profile/Index.aspx?uid=" + CommentData.PostUserId) + "\">" + CommentData.PostUserName + "</a>&nbsp;给我留言写到：</span>");
            }
            else
            {
                sb.Append("<span>我给&nbsp;<a class=\"client_name\" href=\"" + this.GetUrl("/Profile/Index.aspx?uid=" + CommentData.HostUserId) + "\">" + UserBLL.Instance.GetUserInfo(CommentData.HostUserId).UserName + "</a>&nbsp;留言写到：</span>");
            }
            sb.Append("</div>");
            sb.Append("<div class=\"longtextfix\">" + CommentData.Body + "</div>");
            sb.Append("</div>");
            sb.Append("</li>");

            //留言回复
            foreach (CommentInfo replyItem in CommentReplyList)
            {
                sb.Append("<li style=\"width:500px;margin-left:80px; border-left:1px solid #E1E4E6;border-bottom:1px solid #E1E4E6;\"><div style=\"padding-left:20px; text-align:center; float:left;\">");
                string TempUrl2 = "/Profile/Index.aspx?uid=" + replyItem.PostUserId;
                sb.Append("<a href=\"" + this.GetUrl(TempUrl2) + "\"><img width=\"25\" height=\"25\" src=\"" + replyItem.PostAvatar + "\"/>");
                sb.Append("</a></div>");
                sb.Append("<div style=\"margin:0pt 10px 0pt 50px;\">");
                sb.Append("<div>");
                sb.Append("<span style=\"float:right;\">" + replyItem.PostTime.ToString("yyyy-MM-dd  HH:mm") + "</span>");
                sb.Append("<span><a class=\"client_name\" href=\"" + this.GetUrl("/Profile/Index.aspx?uid=" + replyItem.PostUserId) + "\">" + replyItem.PostUserName + "</a>&nbsp;回复到：</span>");
                sb.Append("</div>");
                sb.Append("<div class=\"longtextfix\">" + replyItem.Body + "</div>");

                sb.Append("</div>");
                sb.Append("</li>");
            }

            //回复框
            sb.Append("<li style=\"width:500px;margin-left:80px; border-left:1px solid #E1E4E6;border-bottom:1px solid #E1E4E6;\">");
            sb.Append("<a  href=\"javascript:replyComment(" + CommentData.Id + ");\">回复</a>");
            sb.Append("<div id=\"replybox_" + CommentData.Id + "\" style=\"display:none;margin:0pt 10px 0pt 10px;\">");
            sb.Append("<textarea name=\"replycontent_" + CommentData.Id + "\" id=\"replycontent_" + CommentData.Id + "\" width=\"300px\" height=\"80px\" cols=\"60\" rows=\"6\"></textarea>");
            sb.Append("<p style=\"padding-left:200px\"><input id=\"replybutton\" type=\"button\" value=\"回复\" onclick=\"CommentReplyAdd(" + CommentData.Id + ");\">&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"取消\" onclick=\"replycancell(" + CommentData.Id + ");\"></div>");

            sb.Append("</li>");



            ResponseXML(sb);


        }

        private CommentInfo ValidataForm()
        {

            CommentInfo info = new CommentInfo();
            if (CommentData.PostUserId == UserData.Id)
            {
                info.HostUserId = CommentData.HostUserId;
            }
            else
            {
                info.HostUserId = CommentData.PostUserId;
            }
            info.Body = RequestTools.GetString("replycontent").Trim();
            //string toUserName = UserBLL.Instance.GetUserInfo(info.HostUserId).UserName;
            info.Body = info.Body;
            info.UpdateTime = DateTime.Now;
            info.PostTime = DateTime.Now;
            info.PostUserId = UserData.Id;
            info.PostUserName = UserData.UserName;

            info.IsReply = 1;       //表明是回复
            info.ParentId = CommentData.Id;

            return info;


        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}

